package omm.domain.evolutiongraph;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import omm.domain.CommissionStrategy;
import omm.domain.Quotation;

import org.apache.log4j.Logger;
import org.joda.time.DateTime;

public class QuotationQuantityUnitStrategy implements UnitStrategy {

	private static final Logger logger = Logger.getLogger(QuotationQuantityUnitStrategy.class);
	@Override
	public Map<DateTime, Double> calculateByPeriod(List<Quotation> quotations, PeriodStrategy period,
			CommissionStrategy commition) {
		Map<DateTime, Double> barsQuantity = period.calculatePeriods();

		Iterator<Quotation> it = quotations.iterator();
		while (it.hasNext()) {
			Quotation q = it.next();

			if (commition.conditionToKeep(q.getState())) {
				DateTime modificationDate = q.getModificationDate();

				if (modificationDate != null) {
					for (Map.Entry<DateTime, Double> e : barsQuantity.entrySet()) {
						DateTime dateTime = e.getKey();
						// Comparo si pertenece al periodo
						if (period.isInPeriod(modificationDate, dateTime)) {
							barsQuantity.put(dateTime, e.getValue() + 1);
							break;
						}
					}
				}
			}
		}
		for (Entry<DateTime, Double> e : barsQuantity.entrySet()) {
			logger.debug("=========MES:"+e.getKey()+" CANT:"+e.getValue());
		}
		return barsQuantity;
	}

	@Override
	public Integer calculateMaxYAxis(Integer maxYAxis) {
		return maxYAxis;
	}

	@Override
	public String getTitle(CommissionStrategy commision) {
		return "Cantidad+de+cotizaciones+"+commision.getTitle();
	}

}
